package lex;

public class LexicalAnalysis {
    public static int pos = 0;
    public static String src = "    IF GO DO WHILE 123 ABXCEFG 345+-+445\n\r\t\tasdf";
    public static String token;
    public static String[] keywords = {"BEGIN", "END", "IF", "ELSE", "FOR",};

    static boolean isNumber(char ch) {
        String all = "0123456789";
        return all.contains("" + ch);
    }

    static boolean isLetter(char ch) {
        String all = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        return all.contains("" + ch);
    }

    static boolean isSpace(char ch) {
        String all = " ";
        return all.contains("" + ch);
    }

    static boolean isOperator(char ch) {
        String all = "+-*/%=";
        return all.contains("" + ch);
    }

    static boolean isSeperator(char ch) {
        String all = "()[]{}|\"';,:";
        return all.contains("" + ch);
    }

    static int typeOfChar(char ch) {
        int type;
        if (isSpace(ch))
            type = 0;
        else if (isNumber(ch))
            type = 1;
        else if (isLetter(ch))
            type = 2;
        else if (isOperator(ch))
            type = 3;
        else if (isSeperator(ch))
            type = 4;
        else
            type = 5;
        return type;

    }

    static void lexicalAnalysis() {
        while (pos < src.length()) {
            token = "";
            switch (typeOfChar(src.charAt(pos))) {
                case 0:
                    getNB();
                    break;
                case 1:
                    getNumber();
                    break;
                case 2:
                    getIdentifer();
                    break;
                case 3:
                    getOperator();
                    break;
                case 4:
                case 5:
                    getSeperator();
                    break;
                default:
                    break;
            }
        }

    }

    static void getSeperator() {
        token = "" + src.charAt(pos);
        if (++pos < src.length()) {
            System.out.println("SEPERATOR:\t" + token);
        }
    }

    static void getOperator() {
        token = "" + src.charAt(pos);
        if (++pos < src.length()) {
            System.out.println("OPERERATOR:\t" + token);
        }
    }

    static void getIdentifer() {
        token = "" + src.charAt(pos);
        while (++pos < src.length()
                && (isLetter(src.charAt(pos)) || isNumber(src.charAt(pos)))) {
            token += src.charAt(pos);
        }
        int index = lookUp(token, keywords);
        if (index < 0)
            System.out.println("IDENTIFIER:\t" + token);
        else
            System.out.println("KEYWORDS:\t" + index);
    }

    static int lookUp(String key, String[] table) {
        int index = -1;
        for (int i = 0; i < table.length; i++) {
            if (key == table[i]) {
                index = i;
                break;
            }
        }
        return index;
    }

    static void getNumber() {
        token = "" + src.charAt(pos);
        while (++pos < src.length() && isNumber(src.charAt(pos))) {
            token += src.charAt(pos);
        }
        System.out.println("NUMMBER:\t" + token);
    }

    static void getNB() {
        while (++pos < src.length() && isSpace(src.charAt(pos))
                && pos < src.length())
            ;
    }

    public static void main(String[] args) {
        lexicalAnalysis();
    }
}
